#!/usr/bin/env python
#coding: utf-8

import re

from lib.exception.request import *

class Request:
    """
    Request model class.
    """
    url = "" #完整url
    domain = "" #域名    
    port = 80 #端口
    uri = "" #uri地址
    timeout = 35 #超时时间
    decode = None #编码格式    
    method = "GET" #请求方式
    data = None #请求数据

    def __init__(self, url, timeout, decode=None):
        if url:
            self.url = url
            domain_regex = re.compile(r'http://(.*?)/(.*)')
            port_regex = re.compile(r':(\d+)')
            domain_match = domain_regex.match(url)
            if domain_match:                
                self.domain = domain_match.group(1)
                self.uri = "/%s" % domain_match.group(2)
                if self.uri.endswith('/'):
                    self.uri += "?jdfwkey=gb5tj3" 
                port_match = port_regex.match(domain_match.group(1))
                if port_match:
                    self.port = port_match.group(1)
            self.decode = decode
        else:
            raise RequestInitException("Request url can not be None!")